package com.moter.demo01.juc.basic;


public class SynchronizedDemo {
    public static void main(String[] args) {
        TicketSynchronized ticket = new TicketSynchronized();
        new Thread(()->{ for (int i = 0; i <40 ; i++) ticket.sale();},"A").start();
        new Thread(()->{ for (int i = 0; i <40 ; i++) ticket.sale();},"B").start();
        new Thread(()->{ for (int i = 0; i <40 ; i++) ticket.sale();},"C").start();
    }
}
class TicketSynchronized {
    private int tickets = 30;

    public synchronized void sale(){
        if(tickets>0){
            System.out.println(Thread.currentThread().getName()+"卖出了第"+tickets--+"张票，还剩"+tickets+"张");
        }
    }
}
